package cn.fsw.pethome.org.service.impl;

import cn.fsw.pethome.base.query.Page;
import cn.fsw.pethome.base.service.impl.BaseServiceImpl;
import cn.fsw.pethome.org.domain.Employee;
import cn.fsw.pethome.org.mapper.EmployeeMapper;
import cn.fsw.pethome.org.service.IEmployeeService;
import cn.fsw.pethome.system.mapper.RoleMapper;
import cn.fsw.pethome.user.domain.Logininfo;
import cn.fsw.pethome.user.mapper.LogininfoMapper;
import cn.fsw.pethome.util.MD5Utils;
import cn.fsw.pethome.util.PageList;
import cn.fsw.pethome.util.StrUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Override
    @Transactional
    public void add(Employee employee) {
        this.encrypt(employee);
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setDisable(1);//可用
        logininfo.setType(0);//管理员
        logininfoMapper.add(logininfo);
        employee.setLogininfo_id(logininfo.getId());
        employee.setRole_id(Arrays.asList(22L));
        super.add(employee);
        if(!StringUtils.isEmpty(employee.getRole_id())){
            employeeMapper.addRole(employee.getId(),employee.getRole_id());
        }
    }
    /*Employee和Logininfo同步*/
    @Override
    @Transactional
    public void update(Employee employee) {
        //先删除之前的绑定
        employeeMapper.deleteRole(employee.getId());
        super.update(employee);
        Employee employee1 = super.selectById(employee.getId());
        Long logininfo_id = employee1.getLogininfo_id();
        Logininfo logininfo = logininfoMapper.selectById(logininfo_id);
        Long id = logininfo.getId();//获取之前的id
        BeanUtils.copyProperties(employee1,logininfo);//将修改后的employee复制给logininfo
        logininfo.setId(id);//将之前的id设置回来
        logininfoMapper.update(logininfo);
        if(!StringUtils.isEmpty(employee.getRole_id())){
            employeeMapper.addRole(employee.getId(),employee.getRole_id());
        }
    }

    @Override
    @Transactional
    public void delete(Long id) {
        logininfoMapper.delete(super.selectById(id).getLogininfo_id());
        employeeMapper.deleteRole(id);
        super.delete(id);
    }

   /* @Override
    @Transactional
    public PageList<Employee> pageSelevtAll(Page query) {
        List<Employee> employees = employeeMapper.pageSelevtAll(query);
        for (Employee employee : employees) {

        }
        return super.pageSelevtAll(query);
    }*/

    /*给密码加密*/
    private void encrypt(Employee employee){
        /*随机生成32位*/
        String complexRandomString = StrUtils.getComplexRandomString(32);
        employee.setSalt(complexRandomString);//盐值
        String pwd = MD5Utils.encrypByMd5(employee.getPassword() + complexRandomString);
        employee.setPassword(pwd);

    }

    @Override
    public PageList<Employee> pageSelevtAll(Page query) {
        PageList<Employee> result = new PageList<>();
        result.setTotal(employeeMapper.count(query));
        List<Employee> employees = employeeMapper.pageSelevtAll(query);
        for (Employee employee : employees) {
            employee.setRole_id(roleMapper.selectEmpAndRole(employee.getId()));
        }
        result.setData(employees);
        return result;
    }

    @Override
    public List<Employee> selectAByShopId(Long shopid) {
        return employeeMapper.selectAByShopId(shopid);
    }
}
